<template>
  <view class="fixed bottom-0 left-0 w-full z-111">
    <view class="flex w-full justify-center mb-40rpx" v-if="props.show">
      <view
        class="mx-32rpx bg-#3BBE35 h-96rpx w-full rounded-full flex justify-center items-center text-#ffffff"
        @click="onSave('1')"
        v-if="
          (props.form?.status == '0' ||
            (props.workData?.can_submit == '1' &&
              props.workData?.process_id != '0' &&
              props.form?.staff_id == permission.user.staff_id)) &&
          props.amend
        "
        >暂存</view
      >

      <view
        class="mx-32rpx bg-#3190FF h-96rpx w-full rounded-full flex justify-center items-center text-#ffffff"
        @click="onSave('0')"
        v-if="
          (props.form?.status == '0' ||
            (props.workData?.can_submit == '1' &&
              props.workData?.process_id != '0' &&
              (props.form?.applicant_id || props.form?.staff_id) == permission.user.staff_id)) &&
          props.amend
        "
        >提交审批</view
      >
      <view
        class="mx-32rpx bg-#3190FF h-96rpx w-full rounded-full flex justify-center items-center text-#ffffff"
        v-if="
          props.workData &&
          props.workData?.can_callback == '1' &&
          (props.form?.applicant_id || props.form?.staff_id) == permission.user.staff_id
        "
        @click="onClick('callBack')"
        >撤销</view
      >
      <view
        class="mx-32rpx bg-#3190FF h-96rpx w-full rounded-full flex justify-center items-center text-#ffffff"
        v-if="
          props.workData &&
          props.form?.status == '1' &&
          (props.form?.applicant_id || props.form?.staff_id) == permission.user.staff_id
        "
        @click="onClick('urge')"
        >催办</view
      >
      <view
        class="mx-32rpx bg-#3BBE35 h-96rpx w-full rounded-full flex justify-center items-center text-#ffffff"
        @click="onClick('share')"
        v-if="(props.workData && props.form?.status == '1'||props.workData && props.form?.status == '2') && props.workData?.can_share == '1'"
        >分享</view
      >
      <view
        class="mx-32rpx bg-#3190FF h-96rpx w-full rounded-full flex justify-center items-center text-#ffffff"
        v-if="props.workData && props.workData?.can_review == '1'"
        @click="onClick('approve')"
        >审核</view
      >
      <view
        class="mx-32rpx bg-#3190FF h-96rpx w-full rounded-full flex justify-center items-center text-#ffffff"
        v-if="props.workData && props.workData?.can_feed == '1'"
        @click="onClick('feed')"
        >协办反馈</view
      >
      <view
        class="mx-32rpx bg-#3190FF h-96rpx w-full rounded-full flex justify-center items-center text-#ffffff"
        v-if="
          props.workData &&
          (props.form?.status == '3' || props.form?.status == '5') &&
          (props.form?.applicant_id || props.form?.staff_id) == permission.user.staff_id
        "
        @click="onClick('reset')"
        >重新发起</view
      >
    </view>
    <view class="h-104rpx w-full flex justify-around items-center shadow-t-md" v-if="props.data">
      <view
        class="flex-1 flex justify-center items-center h-full bg-#F7F7F7"
        :class="{ '!bg-white': index == 0 }"
        v-for="(it, index) in props.data"
        :key="index"
        @click="navigateTo(it.path)">
        <image :src="it.icon" class="w-52rpx h-56rpx"></image>
        <text>{{ it.label }}</text>
      </view>
    </view>
  </view>
  <u-modal
    class="relative"
    v-model="show"
    :asyncClose="true"
    :cancelText="cancelText"
    :confirmText="confirmText"
    :title="text"
    :content="content"
    :show-cancel-button="true"
    @confirm="onConfirm"
    @cancel="onCancel">
    <view class="absolute top-2 right-2 p-2" @click="onClose">
      <u-icon name="close"></u-icon>
    </view>
    <view class="slot-content flex justify-center p-4">
      {{ label }}
    </view>
  </u-modal>
</template>

<script setup lang="ts">
import UModal from '@/uni_modules/vk-uview-ui/components/u-modal/u-modal.vue'
import UInput from '@/uni_modules/vk-uview-ui/components/u-input/u-input.vue'
import usePermission from '@/hooks/usePermission'
import { req } from '@/api/req'

const permission = usePermission()
const typeList = [
  { key: 'reset', label: '重新发起' },
  { key: 'urge', label: '催办' },
  { key: 'callBack', label: '撤回' },
  { key: 'approve', label: '审批' },
  { key: 'feed', label: '协办反馈' },
  { key: 'share', label: '分享' },
]
const labelKey = ref('')
const label = ref('')
const show = ref(false)
const cancelText = ref('')
const confirmText = ref('')
const text = ref('')
const content = ref('')
const props = withDefaults(
  defineProps<{
    data?: Record<string, any>[]
    module?: string
    workData?: Record<string, any>
    staff_id?: string
    form?: Record<string, any>
    show: boolean
    //回退到提交人编辑
    amend?: boolean
  }>(),
  {
    show: true,
    amend: false,
  }
)
const emits = defineEmits<{
  (event: 'change', value: string): void
  (event: 'reset', value: string): void
  (event: 'onSave', value: string): void
  (event: 'onLoad', value: string): void
}>()
function navigateTo(url: string) {
  let page = getCurrentPages()
  if (`/${page[page.length - 1].route}` == url) return
  uni.navigateTo({
    url,
  })
}
function onSave(is_draft: string) {
  emits('change', is_draft)
}

function onClose() {
  show.value = false
}

function onClick(key: string) {
  labelKey.value = key
  cancelText.value = '取消'
  confirmText.value = '确定'
  text.value = typeList.find((e) => e.key == key)?.label
  label.value = `是否确定要${text.value}?`
  key == 'approve'
    ? uni.navigateTo({
        url: `/pages/audit/index?id=${props.form?.id}&module=${props.module}&file=${props.form?.file}`,
      })
    : ''
  key == 'feed'
    ? uni.navigateTo({
        url: `/pages/audit/feedback?id=${props.form?.id}&module=${props.module}&file=${props.form?.file}`,
      })
    : ''
  key == 'share'
    ? uni.navigateTo({
        url: `/pages/audit/share?id=${props.form?.id}&module=${props.module}&file=${props.form?.file}`,
      })
    : ''
  key != 'approve' && key != 'feed' && key != 'share' ? (show.value = true) : ''
}

async function onConfirm() {
  let key = labelKey.value
  let info = {
    module: props.module,
    id: props.form?.id,
  }
  switch (key) {
    case 'reset':
      emits('reset', '')
      break
    case 'urge':
      await req('Workflow/remind', info)
      await uni.showToast({ icon: 'none', title: '催办成功', duration: 2000 }).then()
      break
    case 'callBack':
      info.level = 2
      await req('Workflow/set_callback', info)
      await uni.showToast({ icon: 'none', title: '撤回成功', duration: 2000 }).then()
      break
    case 'approve':
      uni.navigateTo({
        url: `/pages/audit/index?id=${props.form?.id}&module=${props.module}`,
      })
      break
  }
  emits('onLoad', '')
  onClose()
}

function onCancel() {
  onClose()
}
</script>

<style scoped lang="scss">
.line {
  transform: rotate(90deg);
}
</style>
